package com.zwr.sheller.shop.controller;

import com.zwr.sheller.core.common.JsonResult;
import com.zwr.sheller.core.common.KQRBusinessException;
import com.zwr.sheller.core.common.KQRExceptionCode;
import com.zwr.sheller.core.pojo.Code;
import com.zwr.sheller.core.pojo.Coupons;
import com.zwr.sheller.shop.service.CodeService;
import com.zwr.sheller.shop.service.CouponsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 *
 *@author Kang
 *@date
 */
@RestController
@RequestMapping("/couponsController")
@Api(tags = "优惠券管理")
@CrossOrigin
public class CouponsController {




    /**
     * -------------------------------------------------------------------------------
     * 优惠码操作
     * --------------------------------------------------------------------------------
     */
    @Autowired
    CodeService codeService;

    @PostMapping("/addCode")
    @ApiOperation("新增优惠码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sort", dataType = "Integer", required = true, paramType = "query", value = "排序"),
            @ApiImplicitParam(name = "nameCode", dataType = "String", required = true, paramType = "query", value = "优惠码代码"),
            @ApiImplicitParam(name = "conditional", dataType = "Integer", required = true, paramType = "query", value = "消费多少钱可以使用 -1：不限制"),
            @ApiImplicitParam(name = "manner", dataType = "Boolean", required = true, paramType = "query", value = "优惠方式0：满减 1：打折"),
            @ApiImplicitParam(name = "mannerList", dataType = "Double", required = true, paramType = "query", value = "优惠力度"),
            @ApiImplicitParam(name = "limit", dataType = "Boolean", required = true, paramType = "query", value = "0：无限制使用 1：不可与商品促销策略一起使用"),
            @ApiImplicitParam(name = "number", dataType = "Integer", required = true, paramType = "query", value = "优惠码数量 -1则表示不限制数量"),
            @ApiImplicitParam(name = "frequency", dataType = "Integer", required = true, paramType = "query", value = "每个优惠码可使用次数"),
            @ApiImplicitParam(name = "state", dataType = "Boolean", required = true, paramType = "query", value = "优惠券状态1：启用 0：不启用"),
            @ApiImplicitParam(name = "timePeriod", dataType = "Boolean", required = true, paramType = "query", value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart", dataType = "String", required = false, paramType = "query", value = "开始时间"),
            @ApiImplicitParam(name = "timeOld", dataType = "String", required = false, paramType = "query", value = "结束时间")
    })
    public Map<String, Object> addCode(Integer sort,String nameCode,Integer conditional,Boolean manner,Double mannerList,Boolean limit ,
     Integer number,Integer frequency,Boolean state,Boolean timePeriod,String timeStart,String timeOld) {

        Code code=new Code();
        code.setSort(sort);
        code.setNameCode(nameCode);
        code.setConditional(conditional);
        code.setManner(manner);
        code.setMannerList(mannerList);
        code.setLimits(limit);
        code.setNumber(number);
        code.setFrequency(frequency);
        code.setState(state);
        code.setTimePeriod(timePeriod);
        if (timePeriod==true){
            code.setTimeStart(Timestamp.valueOf(timeStart));
            code.setTimeOld(Timestamp.valueOf(timeOld));
        }
        Integer add = codeService.add(code);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",add);
        return jsonResult.getMap();


    }

    @PostMapping("/updateCode")
    @ApiOperation("修改优惠码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", dataType = "Long", required = false, paramType = "query", value = "id"),
            @ApiImplicitParam(name = "sort", dataType = "Integer", required = false, paramType = "query", value = "排序"),
            @ApiImplicitParam(name = "nameCode", dataType = "String", required = false, paramType = "query", value = "优惠码代码"),
            @ApiImplicitParam(name = "conditional", dataType = "Integer", required = false, paramType = "query", value = "消费多少钱可以使用 -1：不限制"),
            @ApiImplicitParam(name = "manner", dataType = "Boolean", required = false, paramType = "query", value = "优惠方式0：满减 1：打折"),
            @ApiImplicitParam(name = "mannerList", dataType = "Double", required = false, paramType = "query", value = "优惠力度"),
            @ApiImplicitParam(name = "limit", dataType = "Boolean", required = false, paramType = "query", value = "0：无限制使用 1：不可与商品促销策略一起使用"),
            @ApiImplicitParam(name = "number", dataType = "Integer", required = false, paramType = "query", value = "优惠码数量 -1则表示不限制数量"),
            @ApiImplicitParam(name = "frequency", dataType = "Integer", required = false, paramType = "query", value = "每个优惠码可使用次数"),
            @ApiImplicitParam(name = "state", dataType = "Boolean", required = false, paramType = "query", value = "优惠券状态1：启用 0：不启用"),
            @ApiImplicitParam(name = "timePeriod", dataType = "Boolean", required = true, paramType = "query", value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart", dataType = "String", required = false, paramType = "query", value = "开始时间"),
            @ApiImplicitParam(name = "timeOld", dataType = "String", required = false, paramType = "query", value = "结束时间")
    })
    public Map<String, Object> updateCode(Long id,Integer sort,String nameCode,Integer conditional,Boolean manner,Double mannerList,Boolean limit ,
                                       Integer number,Integer frequency,Boolean state,Boolean timePeriod,String timeStart,String timeOld) {

        Code code=new Code();
        code.setId(id);
        code.setSort(sort);
        code.setNameCode(nameCode);
        code.setConditional(conditional);
        code.setManner(manner);
        code.setMannerList(mannerList);
        code.setLimits(limit);
        code.setNumber(number);
        code.setFrequency(frequency);
        code.setState(state);
        code.setTimePeriod(timePeriod);
        if (timePeriod==true){
            code.setTimeStart(Timestamp.valueOf(timeStart));
            code.setTimeOld(Timestamp.valueOf(timeOld));
        }else {
            code.setTimeOld(null);
            code.setTimeStart(null);
        }
        Integer add = codeService.update(code);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",add);
        return jsonResult.getMap();


    }

    @PostMapping("/deleteByCode")
    @ApiOperation("删除优惠码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",dataType = "map",required = true,paramType = "query",value = "要删除的id")
    })
    public Map<String,Object> deleteByCode(Map<String,Object> map){
        int i=0;
        System.out.println(map);
        List<Object> ids = (List<Object>) map.get("ids");
        for (Object id : ids) {
            Long idl=Long.parseLong(id.toString());
            System.out.println(idl);
            codeService.delete(idl);
            i++;
        }

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("成功删除数据",i+"条");
        return jsonResult.getMap();
    }

    @PostMapping("/selectByCodePage")
    @ApiOperation("优惠码表格及分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "manner",dataType = "Boolean",required = false,paramType = "query",value = "满减类型 0：满金额减折扣 1：满件数折扣"),
            @ApiImplicitParam(name = "timePeriod",dataType = "Boolean",required = false,paramType = "query",value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "name",dataType = "String",required = false,paramType = "query",value = "模糊查询"),
            @ApiImplicitParam(name = "page",dataType = "Integer",required = true,paramType = "query",value = "第几页")

    })
    public Map<String,Object> selectByCodePage(Boolean manner,  Boolean timePeriod,String name,Integer page){
        Integer manner1=null;
        if (manner!=null){
            if (manner==true){
                manner1=1;
            }else {
                manner1=0;
            }
        }
        Integer timePeriod1 = null;
        if (timePeriod!=null){
            if (timePeriod==true){
                timePeriod1=1;
            }else {
                timePeriod1=0;
            }
        }
        List<Map<String, Object>> mapList = codeService.selectCodeByNameByTimeByManner(manner1, timePeriod1, name, page);
        Map map = codeService.selectCodeByNameByTimeByMannerCount(manner1, timePeriod1, name);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",mapList);
        jsonResult.setField("num",map);
        return jsonResult.getMap();
    }

    /**
     * -------------------------------------------------------------------------------
     * 优惠券操作
     * --------------------------------------------------------------------------------
     */

    @Autowired
    CouponsService couponsService;
    @PostMapping("/addCoupons")
    @ApiOperation("新增购物优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sort", dataType = "Integer", required = true, paramType = "query", value = "排序"),
            @ApiImplicitParam(name = "name", dataType = "String", required = true, paramType = "query", value = "优惠券名称"),
            @ApiImplicitParam(name = "conditional", dataType = "String", required = true, paramType = "query", value = "使用条件 "),
            @ApiImplicitParam(name = "manner", dataType = "Boolean", required = true, paramType = "query", value = "优惠方式0：满减 1：打折"),
            @ApiImplicitParam(name = "mannerList", dataType = "Double", required = true, paramType = "query", value = "满足条件优惠"),
            @ApiImplicitParam(name = "limit", dataType = "Boolean", required = true, paramType = "query", value = "0：无限制使用 1：不可与商品促销策略一起使用"),
            @ApiImplicitParam(name = "numberCoupons", dataType = "Integer", required = true, paramType = "query", value = "优惠券总数量"),
            @ApiImplicitParam(name = "state", dataType = "Boolean", required = true, paramType = "query", value = "优惠券状态1：启用 0：不启用"),
            @ApiImplicitParam(name = "timePeriod", dataType = "Boolean", required = true, paramType = "query", value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart", dataType = "String", required = false, paramType = "query", value = "开始时间"),
            @ApiImplicitParam(name = "timeOld", dataType = "String", required = false, paramType = "query", value = "结束时间")
    })
    public Map<String, Object> addCoupons(Integer sort,String name,Integer conditional,Boolean manner,Double mannerList,Boolean limit ,
                                       Integer numberCoupons,Boolean state,Boolean timePeriod,String timeStart,String timeOld) {
        Coupons coupons=new Coupons();
        coupons.setSort(sort);
        coupons.setName(name);
        coupons.setConditional(conditional);
        coupons.setManner(manner);
        coupons.setMannerList(mannerList);
        coupons.setLimits(limit);
        coupons.setNumberCoupons(numberCoupons);
        //购物优惠券
        coupons.setVarieties(true);

        coupons.setState(state);
        coupons.setTimePeriod(timePeriod);
        if (timePeriod==true){
            coupons.setTimeStart(Timestamp.valueOf(timeStart));
            coupons.setTimeOld(Timestamp.valueOf(timeOld));
        }
        Integer add = couponsService.add(coupons);


        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",add);
        return jsonResult.getMap();


    }




    @PostMapping("/addCouponsGift")
    @ApiOperation("新增购物优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sort", dataType = "Integer", required = true, paramType = "query", value = "排序"),
            @ApiImplicitParam(name = "name", dataType = "String", required = true, paramType = "query", value = "优惠券名称"),
            @ApiImplicitParam(name = "conditional", dataType = "String", required = true, paramType = "query", value = "使用条件 "),
            @ApiImplicitParam(name = "manner", dataType = "Boolean", required = true, paramType = "query", value = "优惠方式0：满减 1：打折"),
            @ApiImplicitParam(name = "mannerList", dataType = "Double", required = true, paramType = "query", value = "满足条件优惠"),
            @ApiImplicitParam(name = "limit", dataType = "Boolean", required = true, paramType = "query", value = "0：无限制使用 1：不可与商品促销策略一起使用"),
            @ApiImplicitParam(name = "number", dataType = "Integer", required = true, paramType = "query", value = "赠送券数量"),
            @ApiImplicitParam(name = "total", dataType = "Integer", required = true, paramType = "query", value = "赠送券总数量"),
            @ApiImplicitParam(name = "confined", dataType = "Boolean", required = true, paramType = "query", value = "1:订单付款后发送  0：订单完成后发送"),
            @ApiImplicitParam(name = "state", dataType = "Boolean", required = true, paramType = "query", value = "优惠券状态1：启用 0：不启用"),
            @ApiImplicitParam(name = "timePeriod", dataType = "Boolean", required = true, paramType = "query", value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart", dataType = "String", required = false, paramType = "query", value = "开始时间"),
            @ApiImplicitParam(name = "timeOld", dataType = "String", required = false, paramType = "query", value = "结束时间")
    })
    public Map<String, Object> addCouponsGift(Integer sort,String name,Integer conditional,Boolean manner,Double mannerList,Boolean limit ,
                                          Integer number,Integer total,Boolean confined,Boolean state,Boolean timePeriod,String timeStart,String timeOld) throws KQRBusinessException {
        Coupons coupons=new Coupons();
        coupons.setSort(sort);
        coupons.setName(name);
        coupons.setConditional(conditional);
        coupons.setManner(manner);
        coupons.setMannerList(mannerList);
        coupons.setLimits(limit);
        //购物赠送券
        coupons.setVarieties(false);
        coupons.setNumber(number);
        coupons.setConfined(confined);
        coupons.setTotal(total);
        if (total>number){
            throw new KQRBusinessException(KQRExceptionCode.NOT_NUMBER_MORE);
        }
        coupons.setState(state);
        coupons.setTimePeriod(timePeriod);
        if (timePeriod==true){
            coupons.setTimeStart(Timestamp.valueOf(timeStart));
            coupons.setTimeOld(Timestamp.valueOf(timeOld));
        }
        Integer add = couponsService.add(coupons);


        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",add);
        return jsonResult.getMap();


    }
    @PostMapping("/deleteByCoupons")
    @ApiOperation("删除购物优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",dataType = "map",required = true,paramType = "query",value = "要删除的id")
    })
    public Map<String,Object> deleteByCoupons(Map<String,Object> map){
        int i=0;
        System.out.println(map);
        List<Object> ids = (List<Object>) map.get("ids");
        for (Object id : ids) {
            Long idl=Long.parseLong(id.toString());
            System.out.println(idl);
            couponsService.delete(idl);
            i++;
        }

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("成功删除数据",i+"条");
        return jsonResult.getMap();
    }

    @PostMapping("/selectByCouponsPage")
    @ApiOperation("优惠券表格及分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "manner",dataType = "Boolean",required = false,paramType = "query",value = "满减类型 0：满金额减折扣 1：满件数折扣"),
            @ApiImplicitParam(name = "timePeriod",dataType = "Boolean",required = false,paramType = "query",value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "name",dataType = "String",required = false,paramType = "query",value = "模糊查询"),
            @ApiImplicitParam(name = "page",dataType = "Integer",required = true,paramType = "query",value = "第几页"),
            @ApiImplicitParam(name = "varieties",dataType = "Boolean",required = false,paramType = "query",value = "1:购物优惠券 0：购物赠送券")

    })
    public Map<String,Object> selectByCouponsPage(Boolean manner,  Boolean timePeriod,String name,Integer page,Boolean varieties){
        Integer manner1=null;
        if (manner!=null){
            if (manner==true){
                manner1=1;
            }else {
                manner1=0;
            }
        }
        Integer timePeriod1 = null;
        if (timePeriod!=null){
            if (timePeriod==true){
                timePeriod1=1;
            }else {
                timePeriod1=0;
            }
        }
        Integer varieties1 = null;
        if (varieties!=null){
            if (varieties==true){
                varieties1=1;
            }else {
                varieties1=0;
            }
        }
        List<Map<String, Object>> mapList = couponsService.selectCouponsByNameByTimeByManner(manner1, timePeriod1, name, page, varieties1);
        Map map = couponsService.selectCouponsByNameByTimeByMannerCount(manner1, timePeriod1, name, varieties1);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",mapList);
        jsonResult.setField("num",map);
        return jsonResult.getMap();


    }



    @PostMapping("/updateCoupons")
    @ApiOperation("修改购物优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sort", dataType = "Integer", required = true, paramType = "query", value = "排序"),
            @ApiImplicitParam(name = "name", dataType = "String", required = true, paramType = "query", value = "优惠券名称"),
            @ApiImplicitParam(name = "conditional", dataType = "String", required = true, paramType = "query", value = "使用条件 "),
            @ApiImplicitParam(name = "manner", dataType = "Boolean", required = true, paramType = "query", value = "优惠方式0：满减 1：打折"),
            @ApiImplicitParam(name = "mannerList", dataType = "Double", required = true, paramType = "query", value = "满足条件优惠"),
            @ApiImplicitParam(name = "limit", dataType = "Boolean", required = true, paramType = "query", value = "0：无限制使用 1：不可与商品促销策略一起使用"),
            @ApiImplicitParam(name = "numberCoupons", dataType = "Integer", required = true, paramType = "query", value = "优惠券总数量"),
            @ApiImplicitParam(name = "state", dataType = "Boolean", required = true, paramType = "query", value = "优惠券状态1：启用 0：不启用"),
            @ApiImplicitParam(name = "timePeriod", dataType = "Boolean", required = true, paramType = "query", value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart", dataType = "String", required = false, paramType = "query", value = "开始时间"),
            @ApiImplicitParam(name = "timeOld", dataType = "String", required = false, paramType = "query", value = "结束时间")
    })
    public Map<String, Object> updateCoupons(Integer sort,String name,Integer conditional,Boolean manner,Double mannerList,Boolean limit ,
                                          Integer numberCoupons,Boolean state,Boolean timePeriod,String timeStart,String timeOld) {
        Coupons coupons=new Coupons();
        coupons.setSort(sort);
        coupons.setName(name);
        coupons.setConditional(conditional);
        coupons.setManner(manner);
        coupons.setMannerList(mannerList);
        coupons.setLimits(limit);
        coupons.setNumberCoupons(numberCoupons);
        //购物优惠券
        coupons.setVarieties(true);

        coupons.setState(state);
        coupons.setTimePeriod(timePeriod);
        if (timePeriod==true){
            coupons.setTimeStart(Timestamp.valueOf(timeStart));
            coupons.setTimeOld(Timestamp.valueOf(timeOld));
        }else {
            coupons.setTimeStart(null);
            coupons.setTimeOld(null);
        }
        Integer add = couponsService.update(coupons);


        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",add);
        return jsonResult.getMap();


    }

    @PostMapping("/updateCouponsGift")
    @ApiOperation("修改购物优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sort", dataType = "Integer", required = true, paramType = "query", value = "排序"),
            @ApiImplicitParam(name = "name", dataType = "String", required = true, paramType = "query", value = "优惠券名称"),
            @ApiImplicitParam(name = "conditional", dataType = "String", required = true, paramType = "query", value = "使用条件 "),
            @ApiImplicitParam(name = "manner", dataType = "Boolean", required = true, paramType = "query", value = "优惠方式0：满减 1：打折"),
            @ApiImplicitParam(name = "mannerList", dataType = "Double", required = true, paramType = "query", value = "满足条件优惠"),
            @ApiImplicitParam(name = "limit", dataType = "Boolean", required = true, paramType = "query", value = "0：无限制使用 1：不可与商品促销策略一起使用"),
            @ApiImplicitParam(name = "number", dataType = "Integer", required = true, paramType = "query", value = "赠送券数量"),
            @ApiImplicitParam(name = "total", dataType = "Integer", required = true, paramType = "query", value = "赠送券总数量"),
            @ApiImplicitParam(name = "confined", dataType = "Boolean", required = true, paramType = "query", value = "1:订单付款后发送  0：订单完成后发送"),
            @ApiImplicitParam(name = "state", dataType = "Boolean", required = true, paramType = "query", value = "优惠券状态1：启用 0：不启用"),
            @ApiImplicitParam(name = "timePeriod", dataType = "Boolean", required = true, paramType = "query", value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart", dataType = "String", required = false, paramType = "query", value = "开始时间"),
            @ApiImplicitParam(name = "timeOld", dataType = "String", required = false, paramType = "query", value = "结束时间")
    })
    public Map<String, Object> updateCouponsGift(Integer sort,String name,Integer conditional,Boolean manner,Double mannerList,Boolean limit ,
                                              Integer number,Integer total,Boolean confined,Boolean state,Boolean timePeriod,String timeStart,String timeOld) throws KQRBusinessException {
        Coupons coupons=new Coupons();
        coupons.setSort(sort);
        coupons.setName(name);
        coupons.setConditional(conditional);
        coupons.setManner(manner);
        coupons.setMannerList(mannerList);
        coupons.setLimits(limit);
        //购物赠送券
        coupons.setVarieties(false);
        coupons.setNumber(number);
        coupons.setConfined(confined);
        coupons.setTotal(total);
        if (total>number){
            throw new KQRBusinessException(KQRExceptionCode.NOT_NUMBER_MORE);
        }
        coupons.setState(state);
        coupons.setTimePeriod(timePeriod);
        if (timePeriod==true){
            coupons.setTimeStart(Timestamp.valueOf(timeStart));
            coupons.setTimeOld(Timestamp.valueOf(timeOld));
        }else {
            coupons.setTimeStart(null);
            coupons.setTimeOld(null);
        }
        Integer add = couponsService.update(coupons);


        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",add);
        return jsonResult.getMap();
    }
}
